package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.EmployeeMapper;
import com.hrma.dao.LeaveApplicationMapper;
import com.hrma.entity.LeaveApplication;
import com.hrma.entity.LeaveApplicationDTO;
import com.hrma.entity.LeaveApplicationVO;
import com.hrma.entity.SelectResult;
import com.hrma.entity.exception.StateException;
import com.hrma.services.LeaveApplicationService;
import com.hrma.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeaveApplicationImpl implements LeaveApplicationService {
    private final LeaveApplicationMapper leaveApplicationMapper;
    private final EmployeeMapper employeeMapper;

    public LeaveApplicationImpl(LeaveApplicationMapper leaveApplicationMapper, EmployeeMapper employeeMapper) {
        this.leaveApplicationMapper = leaveApplicationMapper;
        this.employeeMapper = employeeMapper;
    }

    @Override
    public LeaveApplication add(LeaveApplication data) {
//        if(employeeMapper.getstate(UserUtil.getUserId()) == 0){
//            throw new StateException("员工已离职！");
//        }
        data.setEmployeeId(UserUtil.getUserId());
        leaveApplicationMapper.add(data);
        return data;
    }

    @Override
    public void update(LeaveApplication data) {
        if(!leaveApplicationMapper.exist(data.getId())){
            throw new RuntimeException("id字段不存在！");
        }
        if(leaveApplicationMapper.getstate(data.getId()) != 0){
            throw new StateException("当前申请状态不能进行修改！");
        }
        leaveApplicationMapper.update(data);
    }

    @Override
    public List<SelectResult> select() {
        return leaveApplicationMapper.select();
    }

    @Override
    public Page<LeaveApplicationVO> getList(LeaveApplicationDTO data) {
        data.setState(0);
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return leaveApplicationMapper.getList(data);
    }

    @Override
    public Page<LeaveApplicationVO> getPersonalList(LeaveApplicationDTO data) {
        data.setState(0);
        data.setEmployeeId(UserUtil.getUserId());
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return leaveApplicationMapper.getList(data);
    }
}
